#!/bin/python

#
#Xithyl, Test Driven Development for C
#Copyright (C) 2012  Stepan Henek
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from xithyl import logger
from xithyl.Ctypes.basic import *
from xithyl.Ctypes import libc
from xithyl.conditions import basic
from xithyl import actions

logger.addLogger(__name__)

def functional1():
    """
    Create two arrays.
    Iterate the comparations lengths until.
    Use memcmp to compare first to second and vice versa.
    From some point the arrays are equal.
    """

    result = True

    logger.log("Functional 1", __name__)

    logger.log("Init array1", __name__)
    str1 = makeArray(30, 0x0)

    # Test assignment
    result &= basic.arrayEqualsLen([0] * 30, charArrayToList(str1, 30), 30, __name__, True)

    # Set character
    setCharInArray(str1, 10, chr(1))

    # Test the setting
    result &= basic.arrayEqualsLen([0] * 10 + [1] + [0] * 19, charArrayToList(str1, 30), 30, __name__, True)


    logger.log("Init array2", __name__)
    str2 = makeArray(30, 0x0)

    # Set character
    setCharInArray(str2, 11, chr(1))

    # Test the setting
    result &= basic.arrayEqualsLen([0] * 11 + [1] + [0] * 18, charArrayToList(str2, 30), 30, __name__, True)

    # Call memcmp
    for i in range(20)[::-1]:
        logger.log("Iterator: %2d" % i, __name__)
        res = libc.string.memcmp(str1, str2, i)
        if i < 11:
            res &= basic.numbersEquals(res, 0, __name__, True)
        else:
            res &= basic.numbersHigher(res, 0, __name__, True)

        res = libc.string.memcmp(str2, str1, i)
        if i < 11:
            res &= basic.numbersEquals(res, 0, __name__, True)
        else:
            res &= basic.numbersLower(res, 0, __name__, True)


    logger.log("Functional 1 done", __name__)

    return result
